from django.shortcuts import render
from rest_framework.decorators import action

from utils.response import APIResponse
from .models import CourseCategory, Course, CourseChapter
from .serializer import CourseCategorySerializer, CourseSerializer, CourseChapterSerializer
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin, RetrieveModelMixin
from utils.views import CommonListModelMixin
from .page import CommonPageNumberPagination as PageNumberPagination
from rest_framework.filters import OrderingFilter, SearchFilter
from django_filters.rest_framework import DjangoFilterBackend


# Create your views here.
class CourseCategoryView(GenericViewSet, CommonListModelMixin):
    queryset = CourseCategory.objects.all().filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = CourseCategorySerializer


class CourseView(GenericViewSet, ListModelMixin, RetrieveModelMixin):
    queryset = Course.objects.all().filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = CourseSerializer
    # 分页
    pagination_class = PageNumberPagination
    # 排序
    filter_backends = [OrderingFilter, DjangoFilterBackend]
    ordering_fields = ['orders', 'price', 'students']
    # 按课程分类过滤
    filterset_fields = ['course_category']


class CourseChapterView(GenericViewSet, CommonListModelMixin):
    queryset = CourseChapter.objects.all().filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = CourseChapterSerializer
    filter_backends = [DjangoFilterBackend]
    filterset_fields = ['course']


class CourseSearchView(GenericViewSet, ListModelMixin):
    queryset = Course.objects.all().filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = CourseSerializer
    # 分页
    pagination_class = PageNumberPagination
    # 排序
    filter_backends = [SearchFilter]
    # 按课程分类过滤
    search_fields = ['name']


from rest_framework.viewsets import ViewSet  # 跟数据库没有必然的联系，所以继承ViewSet
from libs.alipay_common import alipay, GATEWAY
import uuid


# class PayView(ViewSet):
#     @action(methods=['GET', ], detail=False)
#     def alipay(self, request):
#         trade_no = str(uuid.uuid4())
#         s = alipay.api_alipay_trade_page_pay(
#             out_trade_no=trade_no,
#             total_amount=99,
#             subject='精品内衣',
#             return_url="https://example.com",  # get回调地址
#             notify_url="https://example.com/notify"  # post回调地址
#         )
#         pay_url = GATEWAY + s
#         return APIResponse(pay_url=pay_url)
